using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class admin_EMP_customer : System.Web.UI.Page
{    func fn=new func();
    SqlConnection conn;
    protected void Page_Load(object sender, EventArgs e)
    {
        // lay ket noi
        conn=fn.getConnection();

        if (!IsPostBack)
        {
            if (Request.QueryString["uid"] != null)
            {
                // hien thi panel edit cust
                pnEditCustomer.Visible = true;
                //load info Customer
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter("select * from users where UserID='" + Request.QueryString["uid"].ToString() + "'", conn);
                da.Fill(ds, "cust");
                DataRow dr = ds.Tables["cust"].Rows[0];
                txtFname.Text = dr["FirstName"].ToString();
                txtLname.Text = dr["LastName"].ToString();
                txtPhone.Text = dr["Phone"].ToString();
                txtAddress.Text = dr["Address"].ToString();
                txtUserID.Text = dr["UserID"].ToString();
            }
        }
    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {

        try
        {

            string searchtype = ddlSearchType.SelectedValue;
            string searchvalue = txtSearchValue.Text;
            if (searchvalue == "NumberID")
            {
                try
                {
                    int i = int.Parse(searchvalue);
                    
                }
                catch (Exception ex)
                {
                    searchvalue = "";
                }

            }
        dsCustomer.SelectCommand = @"SELECT     dbo.Branches.*, dbo.Users.*,Trangthai=
    case 
	        when Users.Status=1 then 'OK'
	        when  Users.Status=0 then 'NotOK'
    end  
FROM         dbo.Branches INNER JOIN
                      dbo.Users ON dbo.Branches.BranchID = dbo.Users.BranchID
where Users.Level='3'  and " + searchtype + " ='" + searchvalue + "' ";

            
        }
        catch (Exception ex) {
            showAllCustomer();
        }
        
    }

    
    protected void Button2_Click(object sender, EventArgs e)
    {
        showAllCustomer();
    }

    private void showAllCustomer()
    {
        dsCustomer.SelectCommand = @"SELECT     dbo.Branches.*, dbo.Users.*,Trangthai=
    case 
	        when Users.Status=1 then 'OK'
	        when  Users.Status=0 then 'NotOK'
    end  
    FROM         dbo.Branches INNER JOIN
                          dbo.Users ON dbo.Branches.BranchID = dbo.Users.BranchID
    where Users.Level='3'";
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Response.Redirect("create_customer.aspx");
    }



    protected void Button4_Click(object sender, EventArgs e)
    {
        try
        {
            string str = "update  users set FirstName='" + txtFname.Text + "' , LastName='" + txtLname.Text + "' , Phone='" + txtPhone.Text + "', Address='" + txtAddress.Text + "' where UserID='" + txtUserID.Text + "'";
            SqlCommand comm = new SqlCommand(str, conn);
            conn.Open();
            comm.ExecuteNonQuery();
            conn.Close();
            lblThongbao.Text = " Updated! ";
        }
        catch (Exception ex) {
            lblThongbao.Text = " Update failed! "+ex.Message;
        }
        
    }
}
